package com.wxp.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.modules.annotation.MenuMapping;
import com.modules.annotation.ResourceMapping;
import com.modules.annotation.ResourceMapping.Operation;
import com.modules.persistence.DynamicSpecifications;
import com.modules.web.CRUDController;
import com.wxp.entity.Resource;
import com.wxp.service.ResourceService;

@Controller
@RequestMapping("/resource/resourceInfo")
@MenuMapping(name = "资源信息", operator = "resource10", parentOperator = "larry")
public class ResourceController extends CRUDController{
    private static Logger logger	= LoggerFactory.getLogger(ResourceController.class);

   
    
    @Autowired
    private ResourceService resourceService;

    @RequestMapping("/list")
    @RequiresPermissions ("resource:list")
    @ResourceMapping(name = "资源列表", operation = Operation.MENU)
    public String list(Model model){
        return "resource/resourceList";
    }
    
   
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @RequiresPermissions ("resource:list")
    @ResponseBody
    public Map<String, Object> query(HttpServletRequest request) throws Exception {

        Page page = onPrepareQueryData(request);
        return  transferTo(page);
    }
    
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @RequiresPermissions ("resource:queryList")
    @ResponseBody
    public List<Resource> queryList(HttpServletRequest request) throws Exception {
       List<Resource>  list  = resourceService.findAll();
       return list;
    }
    
    
    
    protected Page onPrepareQueryData(HttpServletRequest request) throws Exception {
    	Specification spec = DynamicSpecifications.bySearchFilter(request);//查询条件(基本查询条件)
    	List<Order> list = new ArrayList<Sort.Order>();
    	Order order2 = new Order(Direction.DESC, "parentOperator"); 
    	Order order1 = new Order(Direction.DESC, "module");  
    	list.add(order1);
    	list.add(order2);
    	Sort sort = new Sort(list);
        PageRequest pageRequest = DynamicSpecifications.buildPageRequest(request,sort);//相关的分页
        Page<Resource> page = resourceService.findAll(spec,pageRequest);
        return page;//用来查询相关的数据
    }

   
    
}
